package com.huawei.secure.android.common.encrypt.aes;

import android.annotation.TargetApi;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.secure.android.common.encrypt.utils.EncryptUtil;
import com.huawei.secure.android.common.encrypt.utils.a;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class AesGcm {
    private static final String TAG = "AesGcm";
    private static final String f = "security:";
    private static final String h = "AES";
    private static final String i = "";
    private static final String l = "AES/GCM/NoPadding";
    private static final int m = 16;
    private static final int n = 12;
    private static final int o = 2;

    private AesGcm() {
    }

    public static String encrypt(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || !a()) {
            return "";
        }
        byte[] l = a.l(str2);
        if (l.length >= 16) {
            return encrypt(str, l);
        }
        Log.e(TAG, "key length is not right");
        return "";
    }

    @TargetApi(19)
    public static String encrypt(String str, byte[] bArr) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16 || !a()) {
            return "";
        }
        byte[] generateSecureRandom = EncryptUtil.generateSecureRandom(12);
        byte[] a = a(str, bArr, generateSecureRandom);
        if (a == null || a.length == 0) {
            return "";
        }
        String f = a.f(generateSecureRandom);
        return f + a.f(a);
    }

    public static String encrypt(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3) || !a()) {
            Log.e(TAG, "gcm encrypt parameter is not right");
            return "";
        }
        byte[] l = a.l(str2);
        byte[] l2 = a.l(str3);
        if (l.length >= 16 && str3.length() >= 12) {
            return encrypt(str, l, l2);
        }
        Log.e(TAG, "key length or iv length is not right");
        return "";
    }

    @TargetApi(19)
    public static String encrypt(String str, byte[] bArr, byte[] bArr2) {
        if (!TextUtils.isEmpty(str) && bArr != null && bArr.length >= 16 && bArr2 != null && bArr2.length >= 12 && a()) {
            return a.f(a(str, bArr, bArr2));
        }
        Log.e(TAG, "gcm encrypt param is not right");
        return "";
    }

    @TargetApi(19)
    private static byte[] a(String str, byte[] bArr, byte[] bArr2) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16 || bArr2 == null || bArr2.length < 12 || !a()) {
            Log.e(TAG, "gcm encrypt param is not right");
            return new byte[0];
        }
        try {
            return encrypt(str.getBytes("UTF-8"), bArr, bArr2);
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "GCM encrypt data error" + e.getMessage());
            return new byte[0];
        }
    }

    @TargetApi(19)
    public static byte[] encrypt(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        if (bArr == null || bArr.length == 0 || bArr2 == null || bArr2.length < 16 || bArr3 == null || bArr3.length < 12 || !a()) {
            Log.e(TAG, "gcm encrypt param is not right");
            return new byte[0];
        }
        try {
            Key secretKeySpec = new SecretKeySpec(bArr2, h);
            Cipher instance = Cipher.getInstance(l);
            instance.init(1, secretKeySpec, e(bArr3));
            return instance.doFinal(bArr);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, "GCM encrypt data error" + e.getMessage());
            return new byte[0];
        }
    }

    @TargetApi(19)
    public static byte[] decrypt(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        if (bArr == null || bArr.length == 0 || bArr2 == null || bArr2.length < 16 || !a()) {
            return new byte[0];
        }
        try {
            Key secretKeySpec = new SecretKeySpec(bArr2, h);
            Cipher instance = Cipher.getInstance(l);
            instance.init(2, secretKeySpec, e(bArr3));
            return instance.doFinal(bArr);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, "GCM decrypt data exception: " + e.getMessage());
            return new byte[0];
        }
    }

    public static byte[] encrypt(byte[] bArr, byte[] bArr2) {
        byte[] generateSecureRandom = EncryptUtil.generateSecureRandom(12);
        return a(generateSecureRandom, encrypt(bArr, bArr2, generateSecureRandom));
    }

    public static byte[] decrypt(byte[] bArr, byte[] bArr2) {
        return decrypt(b(bArr), bArr2, a(bArr));
    }

    private static byte[] a(byte[] bArr, byte[] bArr2) {
        Object obj = new byte[(bArr.length + bArr2.length)];
        System.arraycopy(bArr, 0, obj, 0, bArr.length);
        System.arraycopy(bArr2, 0, obj, bArr.length, bArr2.length);
        return obj;
    }

    private static byte[] a(byte[] bArr) {
        Object obj = new byte[12];
        System.arraycopy(bArr, 0, obj, 0, 12);
        return obj;
    }

    private static byte[] b(byte[] bArr) {
        Object obj = new byte[(bArr.length - 12)];
        System.arraycopy(bArr, 12, obj, 0, bArr.length - 12);
        return obj;
    }

    @TargetApi(19)
    public static String decrypt(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || !a()) {
            return "";
        }
        byte[] l = a.l(str2);
        if (l.length >= 16) {
            return decrypt(str, l);
        }
        Log.e(TAG, "key length is not right");
        return "";
    }

    @TargetApi(19)
    public static String decrypt(String str, byte[] bArr) {
        Exception e;
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16 || !a()) {
            return "";
        }
        try {
            Key secretKeySpec = new SecretKeySpec(bArr, h);
            Cipher instance = Cipher.getInstance(l);
            Object a = a(str);
            Object b = b(str);
            if (TextUtils.isEmpty(a) || TextUtils.isEmpty(b)) {
                Log.e(TAG, "ivParameter or encrypedWord is null");
                return "";
            }
            instance.init(2, secretKeySpec, e(a.l(a)));
            return new String(instance.doFinal(a.l(b)), "UTF-8");
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "GCM decrypt data exception: " + e.getMessage());
            return "";
        } catch (UnsupportedEncodingException e3) {
            e = e3;
            Log.e(TAG, "GCM decrypt data exception: " + e.getMessage());
            return "";
        }
    }

    @TargetApi(19)
    public static String decrypt(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3) || !a()) {
            Log.e(TAG, "gcm decrypt param is not right");
            return "";
        }
        byte[] l = a.l(str2);
        byte[] l2 = a.l(str3);
        if (l.length >= 16 && l2.length >= 12) {
            return decrypt(str, l, l2);
        }
        Log.e(TAG, "key length or iv length is not right");
        return "";
    }

    @TargetApi(19)
    public static String decrypt(String str, byte[] bArr, byte[] bArr2) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16 || bArr2 == null || bArr2.length < 12 || !a()) {
            return "";
        }
        try {
            return new String(decrypt(a.l(str), bArr, bArr2), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "GCM decrypt data exception: " + e.getMessage());
            return "";
        }
    }

    private static String a(String str) {
        if (!TextUtils.isEmpty(str) && str.length() >= 24) {
            return str.substring(0, 24);
        }
        Log.e(TAG, "IV is invalid.");
        return "";
    }

    private static String b(String str) {
        if (TextUtils.isEmpty(str) || str.length() < 24) {
            return "";
        }
        return str.substring(24);
    }

    private static boolean a() {
        return VERSION.SDK_INT >= 19;
    }

    public static String decryptWithCryptHead(String str, byte[] bArr) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16) {
            return "";
        }
        String c = AesCbc.c(str);
        if ("".equals(c)) {
            return "";
        }
        int indexOf = c.indexOf(58);
        if (indexOf >= 0) {
            return decrypt(a.f(a.l(c.substring(indexOf + 1))), bArr, a.l(c.substring(0, indexOf)));
        }
        Log.e(TAG, " gcm cipherText data missing colon");
        return "";
    }

    public static String decryptWithCryptHead(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr2 == null || bArr2.length < 16) {
            return "";
        }
        Object c = AesCbc.c(bArr);
        if (c.length == 0) {
            return "";
        }
        int d = d(c);
        if (d >= 0) {
            byte[] copyOf = Arrays.copyOf(c, d);
            int length = (c.length - copyOf.length) - ":".length();
            Object obj = new byte[length];
            System.arraycopy(c, d + 1, obj, 0, length);
            return decrypt(a.f(obj), bArr2, copyOf);
        }
        Log.e(TAG, " gcm cipherText data missing colon");
        return "";
    }

    private static int d(byte[] bArr) {
        if (bArr[12] == (byte) 58) {
            return 12;
        }
        return -1;
    }

    private static AlgorithmParameterSpec e(byte[] bArr) {
        if (VERSION.SDK_INT < 21) {
            return new IvParameterSpec(bArr);
        }
        return new GCMParameterSpec(DnsConfig.MAX_CACHE_ENTRIES, bArr);
    }
}
